use Test::More tests => 53;
#use Test::More qw(no_plan);

use Firepear::Carrot::Lexer;

BEGIN {
use_ok( 'Firepear::Carrot::Handler::Tag' );
}

my $mu = ['[',']',';',"'",'-'];

my $h = Firepear::Carrot::Handler::Tag->new( mu => $mu );

isa_ok ($h, 'Firepear::Carrot::Handler::Tag');

is ($h->{mu}{o}, '\[', 'open');
is ($h->{mu}{c}, '\]', 'close');
is ($h->{mu}{r}, '\;\;\;', 'rem');
is ($h->{mu}{s}, '\;', 'separator');
is ($h->{mu}{q}, "\\'", 'quote');
is ($h->{mu}{z}{o}, '\[', 'rawopen');
is ($h->{mu}{z}{c}, '\]', 'rawclose');

######################################################## 000 - Normal operation
is ($h->status, 0, 'normal ops - 0');

##################################################################### 'handles'
is ($h->handles('[[document'), 0, 'No, dont we handle triggers');
is ($h->handles('[[[data'), 0, 'No, we dont handle verbatim chunks');
is ($h->handles('[section'), 1, 'Yes, we handle tags');
is ($h->handles('text'), 0, 'No, we dont handle raw text');
is ($h->handles(']]'), 1, 'Yes, we partially consume trigger terminals');
is ($h->handles(']]]'), 1, 'Yes, we partially consume verbatim terminals');
is ($h->handles(']'), 1, 'Yes, we handle tag terminals');

################################################### 001 - Complete node parsing
my $t = "[document title;Foobar] this is a test";
my $l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; # prime
my $node = $h->lex($l);
is (defined $node, 1);
is (ref $node, 'HASH', 'should be a hashref');
is ($l->current, ']', 'Lexer in expected state');
is ($node->{element}, 'document');
is ($node->{refid}, undef);
is ($node->{type}, 'TAG');
is ($node->{finfo}[0], 1, 'file number');
is ($node->{finfo}[1], 1, 'line in file');
is (ref $node->{attrs}, 'ARRAY');
is ($node->{attrs}[0], 'title');
is ($node->{attrs}[1], 'Foobar');
is ($l->fetch, ' ');
is ($l->fetch, 'this');
is ($h->status, 1, 'node processed completely - 1');

$h->status(0);
$t = "bar [document title;Foobar] this is a test";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; $l->fetch; $l->fetch; # prime
$node = $h->lex($l);
is (defined $node, 1);
is (ref $node, 'HASH', 'should be a hashref');
is ($node->{type}, 'TAG');
is ($h->status, 1, 'node processed completely - 1');

$h->status(0);
$t = "bar [document\ntitle;xxx] this is a test";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; $l->fetch; $l->fetch; # prime
$node = $h->lex($l);
is (defined $node, 1);
is (ref $node, 'HASH', 'should be a hashref');
is ($node->{type}, 'TAG');
is ($node->{attrs}[0], 'title');
is ($node->{attrs}[1], 'xxx');
is ($h->status, 1, 'node processed completely - 1');


######################################################### 002 - Partial Parsing
$t = "[foo bar;quux bar2;'this is a test' content starts here ]";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; # prime
$h->status(0);
$node = $h->lex($l);
is (ref $node, 'HASH', 'should be a hashref');
is ($node->{element}, 'foo');
is ($node->{refid}, undef);
is ($node->{attrs}[0], 'bar');
is ($node->{attrs}[1], 'quux');
is ($node->{attrs}[2], 'bar2');
is ($node->{attrs}[3], 'this is a test');
is ($h->status, 2, 'node processed partially - 2');
is ($l->current, ' ');
is ($l->fetch, 'content');

################################################################ 003 - Terminal
$t = "]";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; # prime
$h->status(0);
$node = $h->lex($l);
is ($node, undef);
is ($h->status, 3, 'markup terminal found');
